package appapi.service.impl;

import appapi.Request.Basic.EquipMaintenanItemReq;
import appapi.Request.Basic.EquipSelfchecjItemReq;
import appapi.Vo.EditEquipMainItemVo;
import appapi.Vo.EditEquipSelfItemBatchVo;
import appapi.Vo.EditEquipSelfItemVo;
import appapi.Vo.EditSelfCheckItemVo;
import appapi.entity.auth.RolepermissionEntity;
import appapi.entity.basic.EquipmaintenanitemEntity;
import appapi.service.EquipselfcheckitemService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import appapi.dao.EquipselfcheckitemMapper;
import appapi.entity.basic.EquipselfcheckitemEntity;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请求对象
 *
 * @author luocheng
 * @version 1.0
 * @date 2023/05/27 13:42
 */
@Service
public class EquipselfcheckitemServiceImpl implements EquipselfcheckitemService {

    @Resource
    private EquipselfcheckitemMapper equipselfcheckitemMapper;


    @Override
    public int deleteByPrimaryKey(Long selfItemId) {
        return equipselfcheckitemMapper.deleteByPrimaryKey(selfItemId);
    }


    @Override
    public int insert(EquipselfcheckitemEntity record) {
        return equipselfcheckitemMapper.insert(record);
    }


    @Override
    public EquipselfcheckitemEntity detail(Long selfItemId) {
        return equipselfcheckitemMapper.selectByPrimaryKey(selfItemId);
    }

    @Override
    public List<EquipselfcheckitemEntity> getList(EquipSelfchecjItemReq req) {
        return equipselfcheckitemMapper.queryList(req);
    }

    @Override
    public int count(EquipselfcheckitemEntity record) {
        return equipselfcheckitemMapper.count(record);
    }
    public int updateByPrimaryKeySelective(EquipselfcheckitemEntity record) {
        return equipselfcheckitemMapper.updateByPrimaryKeySelective(record);
    }


    @Override
    public int updateByPrimaryKey(EquipselfcheckitemEntity record) {
        return equipselfcheckitemMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(EditEquipSelfItemBatchVo record)
    {
        EquipSelfchecjItemReq req =new EquipSelfchecjItemReq();
        req.setEquipId(record.getEquipId());
        //获取设备原有自检项目
        List<EquipselfcheckitemEntity> oldList =equipselfcheckitemMapper.queryList(req);
        List<EquipselfcheckitemEntity> addList =new ArrayList<>();
        for (EditEquipSelfItemVo vo:record.getListItem()
        ) {
            //新增
            if(oldList.stream().filter(EquipselfcheckitemEntity -> EquipselfcheckitemEntity.getSelfItemId().equals(vo.getSelfItemId())).count()==0) {
                EquipselfcheckitemEntity entity = new EquipselfcheckitemEntity();
                entity.setEquipId(vo.getEquipId());
                entity.setItemName(vo.getItemName());
                entity.setItemDesc(vo.getItemDesc());
                entity.setCreateTime(LocalDateTime.now());
                entity.setUpdateTime(LocalDateTime.now());
                addList.add(entity);
            }
        }
        //删除原有类型
        List<Long> delList =new ArrayList<>();
        List<String> ids =record.getListItem().stream().map(EditEquipSelfItemVo->EditEquipSelfItemVo.getItemName()).collect(Collectors.toList());
        for (EquipselfcheckitemEntity old:oldList
        ) {
            if(ids.stream().filter(itemName -> itemName.equals(old.getItemName())).count()==0) {
                delList.add(old.getSelfItemId());
            }
        }
        if(delList.size()>0) {
            equipselfcheckitemMapper.deleteBatch(delList);
        }
        if(addList.size()>0) {
            equipselfcheckitemMapper.insertBatch(addList);
        }
        return 1;
    }
}
